<?php
/**################################################################################################**   
# Copyright (c)  2008  Carlos Galindo.   
# Permission is granted to copy, distribute and/or modify this document   
# under the terms of the GNU Free Documentation License, Version 1.2   
# or any later version published by the Free Software Foundation;   
# Provided 'as is' with no warranties, nor shall the autor be responsible for any mis-use of the same.     
# A copy of the license is included in the section entitled 'GNU Free Documentation License'.   
#   
#   CJAX FRAMEWORK 3.0.0RC2                $     
#   ajax made easy with cjax                    
#   -- DO NOT REMOVE THIS --                    
#   -- AUTHOR COPYRIGHT MUST REMAIN INTACT -   
#   CJAX FRAMEWORK  3.0.0RC2               $     
#   Written by: Carlos Galindo                  
#   Website: www.cjax.net                     $      
#   Email: cjxxi@msn.com    
#   Date: 2/12/2007                           $     
#   File Last Changed:  03/24/2009            $     
**####################################################################################################*/   

class extension  extends CoreEvents  {
	
	/**
	 * Makes sure the extension are registered 
	 *
	 * @var boolean function
	 */
	private static $shutdown_register_extensions;
	/**
	 * Holds an array of loaded extensions
	 *
	 * @var array $extensions
	 */
	private static $extensions = array();
	
	/**
	 * Obtain the loaded extensions
	 *
	 * @return array $extensions
	 */
	public function get()
	{
		return self::$extensions;
	}
	
	function init()
	{
		if(!self::$shutdown_register_extensions) {
			register_shutdown_function(array(__CLASS__,'register'));
			self::$shutdown_register_extensions = true;
		}
	}
	
	/**
	 * Add an extension to the extensions stack
	 *
	 * @param string $ext_name
	 * @param string $ext_name
	 * @param string $ext_path
	 * @return boolean
	 */
	public function add($f, $ext_path, $ext_name = 'plugins')
	{
		if(in_array($ext_name,self::$extensions)) return true;
		$extension = array('name' =>$f, 'base' =>$ext_path);
		self::$extensions[$ext_name] =  $extension;
	}
	
	
	function xml($method,$base,$name)
	{
		//TODO
		self::init();
		
		self::add($method,$base,$name);
	/*	self::$extensions[$method] = array(
		'method' => $method,
		'base' => $path,
		'name' => $name,
		'testunit' =>''
		);*/
	}
	
	/**
	 * Register extensions
	 *
	 */
	public static function register() 
	{
		if(empty(self::$extensions)) return true;
		foreach (self::$extensions as $extension) {
			$xml[] = ("<extension_list>$extension</extension_list><base>{$extension['base']}</base>");
		}
		parent::xml(implode($xml));
	}
}